#include "stdafx.h"
#include "Section.h"
#include "ColorDef.h"
#include "SectionDialog.h"


Section::Section(void)
{
}


Section::Section(CWnd* pParent)
	: Device(pParent)
{
	for (int i = 0; i < 2; i++)
		data_.insuType_[i] = NORMAL_INSU;
}


Section::~Section(void)
{
}


IMPLEMENT_SERIAL(Section, Device, 1)


void Section::Serialize(CArchive& ar)
{
	Device::Serialize(ar);

	if (ar.IsStoring())
	{	// storing code
		MapWindowPoints(GetParent(), data_.pt_, SECTION_TOTAL_PT_COUNT);
		ar.Write(&data_, sizeof(data_));
	}
	else
	{	// loading code
		ar.Read(&data_, sizeof(data_));
	}
}


BEGIN_MESSAGE_MAP(Section, Device)
	ON_WM_PAINT()
END_MESSAGE_MAP()


void Section::OnPaint()
{
	CPaintDC dc(this);

	drawSection(dc, data_.pt_, 4);
	drawInsu(dc, data_.insuPt_, data_.insuType_, 2);
	drawSealFrame(dc, data_.sealPt_, 8);

	CPoint connPt[2] = { data_.pt_[0], data_.pt_[3] };
	drawConnectPt(dc, connPt, connectFlag_);
}


void Section::setWindow(const CPoint& pt1, const CPoint& pt2)
{
	data_.pt_[0] = data_.pt_[1] = pt1;
	data_.pt_[2] = data_.pt_[3] = pt2;

	
	setWindow();
}


void Section::setWindow()
{
	setSealFrame(data_.sealPt_, InsuHeight_);
	setInsuPt();

	CRect wndRect = getDevRect(data_.pt_, 4);
	MoveWindow(wndRect);
	GetParent()->MapWindowPoints(this, data_.pt_, SECTION_TOTAL_PT_COUNT);
}


void Section::compressWindow()
{
	CPoint wndPt[8];
	setSealFrame(wndPt, InsuHeight_ + 3, true);
	setCRgnWindow(wndPt, 8);
}


bool Section::setAttr()
{
	SectionDialog dlg(GetParent(), this);
	return dlg.DoModal() == IDOK;
}


bool Section::OnMouseViewMove(const CPoint& viewPt, CPoint& outPt)
{
	CPoint connPt[2] = { data_.pt_[0], data_.pt_[3] };
	return autoConnect(viewPt, connectFlag_, outPt, connPt, 2);
}


void Section::setSealFrame(CPoint pt[], int height, bool bWnd)
{
	CPoint sealPt[3][4];
	for (int i = 0; i < 3; i++)
		getBorder(data_.pt_[i], data_.pt_[i + 1], sealPt[i], height);

	if (data_.pt_[0] == data_.pt_[1])
	{
		pt[0] = pt[7] = bWnd ? sealPt[1][0] - CPoint(1, 0) : sealPt[1][0];
		pt[1] = pt[2] = bWnd ? sealPt[1][1] - CPoint(1, 0) : sealPt[1][1];
	}
	else
	{
		pt[0] = sealPt[0][0];
		pt[1] = sealPt[0][1];
		pt[2] = getCrossPoint(sealPt[0][1], sealPt[0][2], sealPt[1][1], sealPt[1][2]);
		pt[7] = getCrossPoint(sealPt[0][0], sealPt[0][3], sealPt[1][0], sealPt[1][3]);
	}

	if (data_.pt_[2] == data_.pt_[3])
	{
		pt[3] = pt[4] = bWnd ? sealPt[1][2] + CPoint(1, 0) : sealPt[1][2];
		pt[5] = pt[6] = bWnd ? sealPt[1][3] + CPoint(1, 0) : sealPt[1][3];
	}
	else
	{
		pt[3] = getCrossPoint(sealPt[1][1], sealPt[1][2], sealPt[2][1], sealPt[2][2]);
		pt[4] = sealPt[2][2];
		pt[5] = sealPt[2][3];
		pt[6] = getCrossPoint(sealPt[1][0], sealPt[1][3], sealPt[2][0], sealPt[2][3]);
	}
}


void Section::extenDev(CPoint newPt, int connFlag)
{
	MapWindowPoints(GetParent(), data_.pt_, SECTION_TOTAL_PT_COUNT);
	data_.pt_[connFlag == 0 ? 0 : 3] = newPt;
	setWindow();
	compressWindow();
}


void Section::setNamePos(void)
{
	CPoint pt[2] = { data_.pt_[1], data_.pt_[2] };
	if (!bFirstCreate_)
	{
		MapWindowPoints(GetParent(), pt, 2);
	}
	
	CPoint namePt = getNameTopLeft(pt[0], pt[1]);
	name_.setWindow(namePt);
}



void Section::setInsuPt(void)
{
	data_.insuPt_[0] = data_.sealPt_[0];
	data_.insuPt_[1] = data_.sealPt_[1];
	data_.insuPt_[2] = data_.sealPt_[4];
	data_.insuPt_[3] = data_.sealPt_[5];
}
